package com.spring.sunflower.common;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.spring.sunflower.R;
import com.spring.sunflower.photoview.OnOutsidePhotoTapListener;
import com.spring.sunflower.photoview.OnPhotoTapListener;
import com.spring.sunflower.photoview.PhotoView;

import java.util.List;

/**
 * Created by Kevin on 2019-07-01<br/>
 * Describe:<br/>
 */
public class PhotoAdapter extends PagerAdapter {
    private Context context;
    List<String> data;
    private int screenWidth;

    public PhotoAdapter(List<String> data) {
        this.data = data;
    } public PhotoAdapter(Context context,List<String> data) {
        this.data = data;
        this.context = context;
    }

    public PhotoAdapter(Context context, List<String> data, int screenWidth) {
        this.context = context;
        this.data = data;
        this.screenWidth = screenWidth;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.adapter_item_photo_viewer, null);
        PhotoView pv = view.findViewById(R.id.pv_photo_view);
        Glide.with(context)
                .load(data.get(position))
                .placeholder(R.drawable.ic_placeholder)
                .into(pv);
        pv.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view, float x, float y) {
                if (listener != null) {
                    listener.onPhotoClick(position);
                }
            }
        });
        pv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (listener != null) {
                    listener.onPhotoLongClick(position);
                }
                return true;
            }
        });
        pv.setOnOutsidePhotoTapListener(new OnOutsidePhotoTapListener() {
            @Override
            public void onOutsidePhotoTap(ImageView imageView) {
                if (listener != null) {
                    listener.onPhotoClick(position);
                }
            }
        });

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    interface OnPhotoClickListener {
        void onPhotoClick(int position);

        void onPhotoLongClick(int position);
    }

    private OnPhotoClickListener listener;

    public void setOnPhotoClickListener(OnPhotoClickListener l) {
        listener = l;

    }
}